<template>
  <BasicModal @register="registerModal" :title="modalTitle" :width="width" @ok="handleOk" v-bind="$attrs">
    <JUpload ref="uploadRef" :value="value" v-bind="uploadBinds.props" @change="emitValue" @success="handleSuccess" />
  </BasicModal>
</template>

<script lang="ts" setup>
  import { ref, unref, reactive, computed, nextTick } from "vue"
  import { BasicModal, useModalInner } from "/@/components/Modal"
  import { useMessage } from "/@/hooks/web/useMessage"
  import JUpload from "./JUpload.vue"
  import { UploadTypeEnum } from "./upload.data"
  import { propTypes } from "/@/utils/propTypes"

  const { createMessage } = useMessage()
  const emit = defineEmits(["change", "success", "update:value", "register", "ok"])
  const props = defineProps({
    value: propTypes.oneOfType([propTypes.string, propTypes.array]),
    width: propTypes.number.def(520),
  })

  const uploadRef = ref()
  const uploadBinds = reactive({ props: {} as any })
  const modalTitle = computed(() => (uploadBinds.props?.fileType === UploadTypeEnum.image ? "图片上传" : "文件上传"))

  // 注册弹窗
  const [registerModal, { closeModal }] = useModalInner(async (data) => {
    uploadBinds.props = unref(data) || {}
    if ([UploadTypeEnum.image, "img", "picture"].includes(uploadBinds.props?.fileType)) {
      uploadBinds.props.fileType = UploadTypeEnum.image
    } else {
      uploadBinds.props.fileType = UploadTypeEnum.file
    }
    nextTick(() => uploadRef.value.addActionsListener())
  })

  function handleOk() {
    closeModal()
    emit("ok")
  }

  function handleSuccess(value) {
    emit("success", value)
  }

  function emitValue(value) {
    emit("change", value)
    emit("update:value", value)
  }
</script>
